package com.ids.actions;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.ids.bean.EmployeeAttendanceStatistics;
import com.ids.bean.UserAttendanceData;
import com.ids.service.AttendanceScreenService;
import com.ids.service.EmployeeAttendanceScreenService;
import com.ids.service.LeaveStatusService;
import com.ids.util.Constants;
import com.ids.util.Utlity;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class WelcomeScreen extends ActionSupport{

private static final long serialVersionUID = 1L;
private Map<String, EmployeeAttendanceStatistics> employeeAttandence;
private Map<String, Double> leaveStatus;
private Map<Integer,String> attendanceType;
private Map<Integer,String> dependantAttendanceType;
private int attendanceTypeId;
private int dependantAttendanceTypeId;
Calendar cal = new GregorianCalendar();
int currentYear=cal.get(Calendar.YEAR);
int currentMonth=cal.get(Calendar.MONTH) + 1;
int month;
int year;
Float plCount;

public String execute() throws Exception {
	    HttpServletRequest request = ServletActionContext.getRequest();
	    execute(request);
	    return getSpecificMonthData();
	}

 
public void execute(HttpServletRequest request) throws Exception {
	Map<String, Object> session = ActionContext.getContext().getSession();
	String empCode = (String)session.get("empCode");
	leaveStatus = LeaveStatusService.getInstance().getLeaveStatus(empCode);
	Date currentDate = new Date();
	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	String currentFormattedDate = formatter.format(currentDate);
	UserAttendanceData userAttendance = AttendanceScreenService.getInstance().getSelectedAttendance(currentFormattedDate,empCode);
	plCount = AttendanceScreenService.getInstance().getPLCount(empCode);
	setAttendanceTypeId(userAttendance.getAttendanceTypeId());
	setDependantAttendanceTypeId(userAttendance.getDependantAttendanceTypeId());
	Map<Integer,String> attendanceTypes = AttendanceScreenService
		.getInstance().getAttendanceType( empCode);
	Map<Integer,String> dependantAttendanceTypes = AttendanceScreenService
			.getInstance().getDependantAttendanceType(userAttendance.getAttendanceTypeId(), empCode);
	setAttendanceType(attendanceTypes);
	setDependantAttendanceType(dependantAttendanceTypes);
	Map<Integer,String> noOfDays = getNoOfDays();
	request.setAttribute("noOfDays",noOfDays);
	request.setAttribute("dependantAttendanceTypes",dependantAttendanceTypes);
	request.setAttribute("attendanceTypes", attendanceTypes);
	String userAttendanceMessage = (String) session.get(Constants.USER_ATTENDANCE_MESSAGE);
	request.setAttribute(Constants.USER_ATTENDANCE_MESSAGE,userAttendanceMessage);
	session.remove(Constants.USER_ATTENDANCE_MESSAGE);

}
public String getSpecificMonthData() {
	HttpServletRequest request = ServletActionContext.getRequest();
	Map<String, Object> session = ActionContext.getContext().getSession();
	boolean isMultiLevel = false;
	final String empCode = (String) session.get("empCode");
	String monthSelection = request.getParameter("monthSelection");
	String yearSelection = request.getParameter("yearSelection");
	String isMultilevelInReq = request.getParameter("isMultiLevelValue");
	String toBeExported = request.getParameter("toBeExported");

	if(null!=monthSelection){
		month = Integer.parseInt(monthSelection);
		
	}else{
		month =new Date().getMonth() + 1;
		
	}
	if (null!=yearSelection){
		year = Integer.parseInt(yearSelection);
	}else{     
		
		year = new Date().getYear() + 1900;
	}
	if(null!=isMultilevelInReq){
		isMultiLevel = Boolean.parseBoolean(isMultilevelInReq);
	}
	
	employeeAttandence = EmployeeAttendanceScreenService.getInstance()
	.getEmployeeAttandence(empCode,isMultiLevel,month,year);
	if(null!=toBeExported){
	if(toBeExported.equals("yes")){
//		String data = getDataForCSV(employeeAttandence);
		try{
//		String fileName = Utlity.getFile(data);
//		Utlity.downloadFile(fileName, request, response);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
 }
	
	String forward = "success";
	
	return forward;
}



public Map<String, EmployeeAttendanceStatistics> getEmployeeAttandence() {
	return employeeAttandence;
}

public void setEmployeeAttandence(
	Map<String, EmployeeAttendanceStatistics> employeeAttandenceData) {
	this.employeeAttandence = employeeAttandenceData;
}

public int[] getMonths() {
		return	Utlity.displayMonthDays(month, year);
}
public List<String> getWeekDays() {
		return Utlity.displayWeekDays(month, year);
}
public List<String> getAttendanceStatistics() {
	return Utlity.displayAttendanceStatistics();
}

public String getMonthMM(){
	return Utlity.getMonthMM();
}



public Map<Integer, String> getAttendanceType() {
	return attendanceType;
}


public void setAttendanceType(Map<Integer, String> attendanceType) {
	this.attendanceType = attendanceType;
}


public Map<Integer, String> getDependantAttendanceType() {
	return dependantAttendanceType;
}


public void setDependantAttendanceType(
		Map<Integer, String> dependantAttendanceType) {
	this.dependantAttendanceType = dependantAttendanceType;
}


public int getAttendanceTypeId() {
	return attendanceTypeId;
}

public void setAttendanceTypeId(int attendanceTypeId) {
	this.attendanceTypeId = attendanceTypeId;
}

public int getDependantAttendanceTypeId() {
	return dependantAttendanceTypeId;
}

public void setDependantAttendanceTypeId(int dependantAttendanceTypeId) {
	this.dependantAttendanceTypeId = dependantAttendanceTypeId;
}


public Map<String, Double> getLeaveStatus() {
	return leaveStatus;
}


public void setLeaveStatus(Map<String, Double> leaveStatus) {
	this.leaveStatus = leaveStatus;
}

public String getCurrentDate(){
	Date date = new Date();
	SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy");
	  return  formatter.format(date.getTime());
	  
}

public int getDefaultAttendanceId(){
	 return Constants.ATTENDANCE_FULL_SHIFT;
}

public Map<Integer, String> getMonthsList() {
	Map<Integer, String> monthsList = new TreeMap<Integer, String>();
	monthsList.put(1, "January");
	monthsList.put(2, "February");
	monthsList.put(3, "March");
	monthsList.put(4, "April");
	monthsList.put(5, "May");
	monthsList.put(6, "June");
	monthsList.put(7, "July");
	monthsList.put(8, "August");
	monthsList.put(9, "September");
	monthsList.put(10, "October");
	monthsList.put(11, "November");
	monthsList.put(12, "December");
	
	return monthsList;
}

public Map<Integer, String> getYearsList() {
	Map<Integer, String> monthsList = new TreeMap<Integer, String>();
	for(int year=2011;year<2022;year++){
		monthsList.put(year, String.valueOf(year));
	}
		return monthsList;
}
public Map<Integer,String> getNoOfDays() {
	Map<Integer, String> noOfDays = new TreeMap<Integer, String>();
	for(int count=3;count<=plCount && count<=15 ;count++){
		noOfDays.put(count, String.valueOf(count));
	}
	return noOfDays;
//	return Utlity.getNoOfDays();
}

public int getCurrentMonth() {
	return currentMonth;
}


public void setCurrentMonth(int currentMonth) {
	this.currentMonth = currentMonth;
}


public int getCurrentYear() {
	return currentYear;
}


public void setCurrentYear(int currentYear) {
	this.currentYear = currentYear;
}
public String getDatepick() {
	Date date = new Date();
	SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
	  return  formatter.format(date.getTime());
}
}
